package com.itheima.admin.controller;

import com.itheima.PageVo;
import com.itheima.Result;
import com.itheima.admin.dto.RoleAuthDto;
import com.itheima.admin.dto.RoleDto;
import com.itheima.admin.dto.RolePageDto;
import com.itheima.admin.service.IRoleService;
import com.itheima.admin.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
@Api("管理后台角色模块接口")
@CrossOrigin
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @PostMapping("/list")
    @ApiOperation("分页查询角色")
    public PageVo<RoleVo> queryByPage(@RequestBody RolePageDto rolePageDto){
       return roleService.queryByPage(rolePageDto);
    }

    @GetMapping("/listall")
    @ApiOperation("查询所有角色")
    public List<RoleVo> queryAll(){
      return roleService.queryAll();
    }

    @PostMapping("/add")
    @ApiOperation("添加角色")
    public Result addRole(@RequestBody RoleDto roleDto){
        boolean result = roleService.addRole(roleDto);

        return new Result(result,null,result?"添加角色成功":"添加角色失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除角色")
    public Result deleteRole(@PathVariable("id") String roleId){

        boolean result = roleService.deleteRole(roleId);

        return new Result(result,null,result?"删除角色成功":"删除角色失败");
    }

    @PostMapping("/auth")
    @ApiOperation("角色授权")
    public Result roleAuth(@RequestBody RoleAuthDto roleAuthDto){
        boolean result = roleService.roleAuth(roleAuthDto);

        return new Result(result,null,result?"角色授权成功":"角色授权失败");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询角色信息")
    public RoleVo queryById(@PathVariable("id") String roleId){
        return roleService.queryById(roleId);
    }

    @PutMapping("/edit")
    @ApiOperation("修改角色信息")
    public Result editRole(@RequestBody RoleVo roleVo){
        boolean result = roleService.editRole(roleVo);

        return new Result(result,null,result?"角色修改成功":"角色修改失败");
    }

    @GetMapping("/{id}/modules")
    @ApiOperation("查询角色对应的模块")
    public Result queryModulesById(@PathVariable("id") String roleId){
        List<String> list = roleService.queryModulesById(roleId);
        return new Result(list.size()>0, list, list.size()>0?"修改角色成功":"修改角色失败");
    }
}
